package com.ben.dao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.ben.bean.Message;
import com.ben.db.DBAccess;

public class MessageDAO {

	public List<Message> queryMessageList(String command, String description){
		List<Message> messageList = new ArrayList<Message>();
		DBAccess dbAccess = new DBAccess();
		SqlSession sqlSession = null;
		try {
			sqlSession = dbAccess.getSqlSession();
			//执行sql语句
			Message message = new Message();
			message.setCommand(command);
			message.setDescription(description);
			System.out.println(command);
			System.out.println(description);
			messageList = sqlSession.selectList("Message.queryMessageList", message);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (sqlSession != null) {
				sqlSession.close();				
			}
		}
		return messageList;
	}
	
	public void deleteOne(int id){
		DBAccess dbAccess = new DBAccess();
		SqlSession sqlSession = null;
		try {
			sqlSession = dbAccess.getSqlSession();
			//执行sql语句
			sqlSession.delete("Message.deleteOne", id);
			sqlSession.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (sqlSession != null) {
				sqlSession.close();				
			}
		}
	}
	
	public void deleteBatch(List<Integer> ids){
		DBAccess dbAccess = new DBAccess();
		SqlSession sqlSession = null;
		try {
			sqlSession = dbAccess.getSqlSession();
			//执行sql语句
			sqlSession.delete("Message.deleteBatch", ids);
			sqlSession.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (sqlSession != null) {
				sqlSession.close();				
			}
		}
	}
	
}
